/**
 * 组织机构
 */
import { toTree } from './helper';

interface OrganizationItem {
  id: string;
  text: string;
  expanded?: boolean;
  leaf?: boolean;
  attributes: Record<string, any>;
  children?: OrganizationItem[];
}

export interface OrgNode {
  title: string;
  value: string;
  data: Record<string, any>;
  children: OrgNode[];
}

export async function getDepartList() {
  const res = {
    success: true,
    items: [
      {
        id: '2',
        text: '众智鸿图测试',
        attributes: {
          type: '1',
          code: '1-1',
          ecode: 'zzhtcs',
          level: '1',
          info: '测试机构管理',
          orderby: '0',
          visible: '',
        },
        expanded: true,
        children: [
          {
            id: '163',
            text: '测试11',
            attributes: {
              type: '1',
              code: '1-1-1',
              ecode: 'zzhtcs',
              level: '2',
              info: '',
              orderby: '0',
              visible: '',
            },
            children: [
              {
                id: '1162',
                text: '88',
                attributes: {
                  type: '1',
                  code: '1-1-1-0',
                  ecode: 'zzhtcs',
                  level: '3',
                  orderby: '999',
                },
                leaf: true,
              },
            ],
          },
          {
            id: '161',
            text: '测试',
            attributes: {
              type: '1',
              code: '1-1-0',
              ecode: 'zzhtcs',
              level: '2',
              info: '结构测试',
              orderby: '121',
            },
            children: [
              {
                id: '162',
                text: '222',
                attributes: {
                  type: '1',
                  code: '1-1-0-0',
                  ecode: 'zzhtcs',
                  level: '3',
                  info: '222',
                  orderby: '121',
                },
                children: [
                  {
                    id: '164',
                    text: '111',
                    attributes: {
                      type: '1',
                      code: '1-1-0-0-0',
                      ecode: 'zzhtcs',
                      level: '4',
                      orderby: '0',
                    },
                    leaf: true,
                  },
                ],
              },
              {
                id: '1161',
                text: '453455',
                attributes: {
                  type: '1',
                  code: '1-1-0-1',
                  ecode: 'zzhtcs',
                  level: '3',
                  orderby: '999',
                },
                leaf: true,
              },
            ],
          },
          {
            id: '1165',
            text: '测试区',
            attributes: {
              type: '1',
              code: '1-1-2',
              ecode: 'zzhtcs',
              level: '2',
              info: '测试区',
              orderby: '999',
            },
            leaf: true,
          },
          {
            id: '1166',
            text: '测试区1',
            attributes: {
              type: '1',
              code: '1-1-3',
              ecode: 'zzhtcs',
              level: '2',
              info: '测试区',
              orderby: '999',
            },
            leaf: true,
          },
          {
            id: '1167',
            text: '2021测试',
            attributes: {
              type: '1',
              code: '1-1-11',
              ecode: 'zzhtcs',
              level: '2',
              orderby: '999',
            },
            leaf: true,
          },
        ],
      },
    ],
  };
  const ss: OrganizationItem[] = res.items;
  const nodes = toTree<OrganizationItem, OrgNode>(ss, (n) => {
    return {
      title: n.text,
      data: n.attributes,
      value: n.id,
      children: [],
    };
  });
  return {
    code: 0,
    data: nodes,
    msg: '获取组织成功',
  };
}
